package indi.caibo.service_edu.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import indi.caibo.commonutils.JwtUtils;
import indi.caibo.commonutils.R;
import indi.caibo.service_edu.entity.EduCourse;
import indi.caibo.service_edu.entity.vo.chapter.ChapterVo;
import indi.caibo.service_edu.entity.vo.course.CourseQueryVo;
import indi.caibo.service_edu.entity.vo.course.CourseFrontVo;
import indi.caibo.service_edu.service.EduChapterService;
import indi.caibo.service_edu.service.EduCourseService;
import indi.caibo.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author Bo Cai
 * @version 1.0
 * @date 2021/12/7 15:04
 */
@Api(tags = "前台课程模块")
@RestController
@RequestMapping("/edu-service/course-front")
public class CourseFrontController {
    @Autowired
    private EduCourseService eduCourseService;
    @Autowired
    private EduChapterService eduChapterService;

    @ApiOperation(value = "分页课程列表")
    @PostMapping(value = "{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQueryVo courseQuery) {
        Page<EduCourse> pageParam = new Page<EduCourse>(page, limit);
        Map<String, Object> map = eduCourseService.pageListFront(pageParam, courseQuery);
        return R.ok().data(map);
    }
    @ApiOperation(value = "分页已收藏课程列表")
    @PostMapping(value = "collected/{page}/{limit}")
    public R collectedPageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQueryVo courseQuery,
            HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        Page<EduCourse> pageParam = new Page<>(page, limit);
        Map<String, Object> map = eduCourseService.collectedPageListFront(pageParam, courseQuery, memberId);
        return R.ok().data(map);
    }

    @ApiOperation(value = "根据ID查询课程")
    @GetMapping(value = "{courseId}")
    public R getById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId) {
        //查询课程信息和讲师信息
        CourseFrontVo courseFrontVo = eduCourseService.selectInfoWebById(courseId);
        //查询当前课程的章节信息
        List<ChapterVo> chapterVideoList = eduChapterService.nestedList(courseId);
        return R.ok().data("courseFrontVo", courseFrontVo).data("chapterVideoList", chapterVideoList);
    }
}
